<?php 
include("../lang/cn/company_type_config.php");
include_once("../module/product/includes/plugin_tarento_class.php");
$tarento = new Tarento();

if($_POST['act'] && $_POST['act']=='bind'){
	$userId = $_POST['member_id'];
	if(!($userId>0)){
		echo '用户不存在';exit;
	}
	$code = trim($_POST['invite_code']);
	$sql = "select relations,level from ".TARENTO." where invite_code='{$code}' limit 1";
	$db->query($sql);
	$res = $db->fetchRow();
	if(empty($res['relations'])){
		echo "<script>alert('无效邀请码');</script>";
		msg('?m=member&s=bind_invitate_code.php&userid='.$userId);
		exit;
	}
	$level = $res['level']+1;
	$inviteCode = $tarento->buildInviteCode($userId);
	$sql = "update ".TARENTO." set relations='{$res['relations']}{$userId},',level='{$level}',invite_code='{$inviteCode}' where member_id={$userId}";
	$db->query($sql);
	//更新结算表关系字段
	$sql = "update ".TARENTOSALESFLOW." set relations='{$res['relations']}{$userId},',level='{$level}' where member_id={$userId} and flow_time_str='".date('Ym')."'";
	$db->query($sql);
	echo "<script>alert('绑定成功');window.parent.main.document.location.reload();window.parent.DialogManager.close('bind_invitate_code');</script>";
}
$member_id = $_GET['userid'];
if(!($member_id>0)){
	echo '用户不存在';exit;
}
$tpl->assign('member_id',$member_id);
$tpl->display('bind_invitate_code.htm');
?>